package www.woniuxy.com.class112.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
//import www.woniuxy.com.class112.dao.RoleMapper;
//import www.woniuxy.com.class112.dao.SpecialAgentsMapper;
//import www.woniuxy.com.class112.dao.UserInfosMapper;
//import www.woniuxy.com.class112.entity.Role;
//import www.woniuxy.com.class112.entity.SpecialAgents;
//import www.woniuxy.com.class112.entity.SpecialAgentsExample;
//import www.woniuxy.com.class112.entity.UserInfos;
import www.woniuxy.com.class112.dao.RoleMapper;
import www.woniuxy.com.class112.dao.SpecialAgentsMapper;
import www.woniuxy.com.class112.dao.UserInfosMapper;
import www.woniuxy.com.class112.dao.UserRoleMapper;
import www.woniuxy.com.class112.entity.*;
import www.woniuxy.com.class112.param.userInfo.SelectAgentInfoPageParam;
import www.woniuxy.com.class112.param.userInfo.addAgentParam;
import www.woniuxy.com.class112.service.AgentService;
import www.woniuxy.com.class112.util.MD5Util;
import www.woniuxy.com.class112.util.Properties;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AgentServiceImpl implements AgentService {
    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private UserInfosMapper userInfosMapper;
    @Resource
    private SpecialAgentsMapper specialAgentsMapper;
    @Resource
    private RoleMapper roleMapper;
    @Override
    public PageInfo<SpecialAgents> selectAgentInfo(SelectAgentInfoPageParam param) throws Exception{
        Page<SpecialAgents> page = PageHelper.startPage(param.getPageIndex()==null?1:param.getPageIndex(), param.getPageSize()==null?5:param.getPageSize());
        SpecialAgentsExample example = new SpecialAgentsExample();
        SpecialAgentsExample.Criteria criteria = example.createCriteria();
        if (param.getName()!=null){
            criteria.andNameLike("%"+param.getName()+"%");
        }
        if (param.getPhone()!=null){
            criteria.andPhoneLike("%"+param.getPhone()+"%");
        }
        specialAgentsMapper.selectByExample(example);
        return new PageInfo<SpecialAgents>(page);
    }

    @Override
    public void addAgent(addAgentParam param) throws Exception{
        SpecialAgentsExample example = new SpecialAgentsExample();
        SpecialAgentsExample.Criteria c1 = example.createCriteria();
        c1.andPhoneEqualTo(param.getPhone());
        List<SpecialAgents> list = specialAgentsMapper.selectByExample(example);
        if(list.size()>0)throw new Exception("手机号重复");
        SpecialAgents SA = BeanUtil.toBean(param, SpecialAgents.class);
        UserInfos userInfos = new UserInfos();
        userInfos.setUserStatr(0L);
        String s = MD5Util.md5(param.getUserPass(), param.getUserName());
        userInfos.setPassword(s);
        userInfos.setUserName(param.getUserName());
        userInfos.setRoleId(Properties.ROLE_AGENT);
        userInfos.setPhone(param.getPhone());
        userInfosMapper.insert(userInfos);
        SA.setUserId(userInfos.getId());
        specialAgentsMapper.insert(SA);
        UserRole userRole = new UserRole();
        userRole.setUserId(userInfos.getId());
        userRole.setRoleId(Properties.ROLE_AGENT);
        userRoleMapper.insert(userRole);
    }

    @Override
    public List<SpecialAgents> adminselectAgentInfo(String name) {
        SpecialAgentsExample example = new SpecialAgentsExample();
        SpecialAgentsExample.Criteria criteria = example.createCriteria();
        if (name.isEmpty()){
            criteria.andNameLike("%"+name+"%");
        }
        List<SpecialAgents> specialAgents = specialAgentsMapper.selectByExample(example);
        return specialAgents;
    }
}
